//app.js

/*
  广告id存放在globalData中，请注意查收
*/ 
App({

  onLaunch: function () {
    var that = this;
    var url = this.globalData.ApiUrl;
    // // 判断是否授权登录
    let key = wx.getStorageSync('key');
    //如果没有key，则是没有登录，那么跳转到登录页
    if (!key) {
      that.goLoginPageTimeOut();
      return
    } else {
      // 检验登录状态--如果key不相同，则重新登录
      wx.request({
        url: url + '/mobile/index.php?act=login&op=verify_token',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        dataType: 'json',
        method: 'post',
        data: {
          key: key
        },
        success: (res) => {
          if (!res.data.code == 200) {
            wx.showModal({
              title: '提示',
              content: res.data.error,
              success: function (res) {
                if (res.confirm) {
                  wx.reLaunch({
                    url: '/pages/authorize/index',
                  });
                }
              }
            });
          };
          if (res.data.code == 200) {
            if (res.data.datas.login_status == false) {
              wx.reLaunch({
                url: '/pages/authorize/index',
              });
            }
          }
        },
        fail: () => {
          wx.showToast({
            title: '请求超时，请重试',
            icon: 'none',
            duration: 2000
          })
        }
      });

    }

    var that = this;
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || [];
    logs.unshift(Date.now());
    wx.setStorageSync('logs', logs);
  },
  goLoginPageTimeOut: function () {
    wx.navigateTo({
      url: "/pages/authorize/index", //暂时屏蔽掉，如果没有授权则跳转到授权页面
    });
  },
  globalData: {
    userInfo: null,
    ApiUrl: 'https://api.zhuisushangcheng.com',
    index_banner: 42, 
    index_share: 43, 
    discount_red: 46, 
    uer_center: 45,
  },
  //用于保存formId的函数，提交订单和领取红包时，需要用表单提交来获取formId 。formId是用来发送服务通知的
  saveFormId: function (formId) {
    var key = wx.getStorageSync('key');
    var api_url = this.globalData.ApiUrl;
    wx.request({
      url: `${api_url}/mobile/index.php?act=member_account&op=save_formid`,
      data: { "key": key, "formid": formId }
    });
  },

  onShow: function () {
    var that = this
    var url = this.globalData.ApiUrl;
    wx.login({
      success: (res) => {
        if (res.code) {
          wx.request({
            url: url + '/mobile/index.php?act=connect&op=get_session_key',
            data: {
              code: res.code,
              applet_type: 'applet_wx'
            },
            header: {
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            dataType: 'json',
            method: 'post',
            success: (res) => {
              if (res.data.code == 200) {
                wx.setStorageSync('openid', res.data.datas.openid); //小程序openid
                wx.setStorageSync('login_secret', res.data.datas.login_secret); //登录秘钥
                wx.setStorageSync('session_key', res.data.datas.session_key); //会话密钥session_key
                wx.setStorageSync("unionid", res.data.datas.unionid);
              } else {
                wx.showToast({
                  title: res.data.datas.error,
                  icon: 'none',
                  duration: 2000
                });
              }
            },
            fail: () => {
              wx.showToast({
                title: '请求超时，请重试',
                icon: 'none',
                duration: 2000
              })
            }
          });
        }
      }
    });
  },
})
